package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;



import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.GongYi;

public class GongYiRowMapper implements RowMapper<GongYi>{
    private int type;

    public static final int GONGYI_AND_NUM = 1;
    
    public GongYiRowMapper() {
    }
    
    public GongYiRowMapper(int type) {
        this.type = type;
    }


    @Override
    public GongYi mapRow(ResultSet rs, int rowNum) throws SQLException {
        GongYi gy = new GongYi();
        switch (type) {
            case GONGYI_AND_NUM:
                gy = parseGongYi(new GongYi(), rs);
                gy.setGy_num(rs.getInt("gy_num"));
                break;
            default:
                gy = parseGongYi(new GongYi(), rs);
        }
        return gy;
    }

    public static GongYi parseGongYi(GongYi gy, ResultSet rs)
        throws SQLException {
        return parseGongYi(gy, rs, null);
    }

    public static GongYi parseGongYi(GongYi gy, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            gy.setGy_id(rs.getLong(su + "gy_id"));
        } catch (SQLException e) {
            gy.setGy_id(null);
        }
        try {
            gy.setGy_name(rs.getString(su + "gy_name"));
        } catch (SQLException e) {
            gy.setGy_name(null);
        }
      
        return gy;
    }

    public static Map<String, Object> getPar(Map<String, Object> par,
            GongYi gy) {
        par.put("gy_id", gy.getGy_id());
        par.put("gy_name", gy.getGy_name());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            GongYi gy) {
        args.put("gy_id", gy.getGy_id());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final GongYi gy) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(gy),
                        new String[] { "gy_id" });
                parsePS(ps, gy);
                return ps;
            }

        };
    }

    private static String parseSQL(GongYi gy) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into userapp_gongyi(");
        int count = 0;
        if (null != gy.getGy_id()) {
            sql.append("gy_id,");
            count++;
        }
        if (null != gy.getGy_name()) {
            sql.append("gy_name,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, GongYi gy)
        throws SQLException {
        int count = 1;
        if (null != gy.getGy_id()) {
            ps.setLong(count++, gy.getGy_id());
        }
        if (null != gy.getGy_name()) {
            ps.setString(count++, gy.getGy_name());
        } 

    }

}
